package com.tongyi.shop.common.pojo.vo;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tongyi.shop.common.enums.StatusCodeEnum;
import lombok.Data;


/**
 * 描述: 方法返回信息
 * @Author: liurong
 * @Date: 2019/12/26 17:10
 */
public class BaseResponse<T> {
    private Integer code;
    private String msg;
    private T data;
    private DataPage page;

    public BaseResponse() {
    }

    public BaseResponse(StatusCodeEnum statusCodeEnum) {
        this.code=statusCodeEnum.getCode();
        this.msg=statusCodeEnum.getMsg();
    }

    public BaseResponse(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public BaseResponse(T data, StatusCodeEnum statusCodeEnum) {
        this.data = data;
        this.code=statusCodeEnum.getCode();
        this.msg=statusCodeEnum.getMsg();
    }

    public BaseResponse(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public void statusMsg(StatusCodeEnum statusCodeEnum){
        this.code=statusCodeEnum.getCode();
        this.msg=statusCodeEnum.getMsg();
    }

    public static BaseResponse ofMessage(int code, String message){
        return new BaseResponse(code,message);
    }

    public static BaseResponse ofSuccess(Object data){
        return new BaseResponse(data,StatusCodeEnum.Success);
    }

    public static BaseResponse success(String message){
        return new BaseResponse(StatusCodeEnum.Success.getCode(), message);
    }

    public static BaseResponse fail(String message){
        return new BaseResponse(StatusCodeEnum.Fail.getCode(),message);
    }

    public static BaseResponse ofStatus(StatusCodeEnum status){
        return new BaseResponse(status.getCode(),status.getMsg());
    }

    public void setPageForMybatisPage(IPage iPage){
        DataPage page = new DataPage();
        page.setPageCount((int) iPage.getPages());
        page.setTotal((int) iPage.getTotal());
        this.setPage(page);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public DataPage getPage() {
        return page;
    }

    public void setPage(DataPage page) {
        this.page = page;
    }

}
